def is_npu_idle(device_path="/sys/class/bm-tpu/bm-tpu0/device/npu_usage"):
    """
    检查NPU是否处于空闲状态
    返回: True(空闲)/False(忙碌)/None(检测失败)
    """
    try:
        with open(device_path, 'r') as f:
            for line in f:
                # 解析每行数据示例: "core 0 usage:23 avusage:45"
                if 'usage:' in line:
                    core_usage = int(line.split('usage:')[1].split()[0])
                    if core_usage > 0:
                        return False
        return True  # 所有核心使用率均为0
    except FileNotFoundError:
        print(f"错误：设备文件 {device_path} 不存在")
    except Exception as e:
        print(f"检测异常：{str(e)}")
    return None  # 检测失败

# 使用示例
# if __name__ == "__main__":
#     import time
    
#     while True:
#         status = is_npu_idle()
#         if status is True:
#             print("NPU空闲，程序已执行完毕")
#             # break
#         elif status is False:
#             print("NPU使用中...")
#         else:
#             print("检测失败，重试中...")
        
#         time.sleep(2)  # 每2秒检测一次